#include "stdafx.h"
#include "Framework\lego_3Dview.h"
#include "gladdon\trackball.h"
#include "DLG_Camera_Setting.h"

IMPLEMENT_DYNAMIC(CDLG_CameraSetting, CDialog)

CDLG_CameraSetting::CDLG_CameraSetting(CWnd* pParent /*=NULL*/)
	: CDialog(CDLG_CameraSetting::IDD, pParent)
{	m_pView = NULL;	}

CDLG_CameraSetting::CDLG_CameraSetting(CLego_3Dview *pView){
	m_pView = pView;	
}

CDLG_CameraSetting::~CDLG_CameraSetting(){
}

BOOL CDLG_CameraSetting::Create( void ){
	if(CDialog::Create(CDLG_CameraSetting::IDD)){
		Axis[_X].set(1.0, .0, .0);
		Axis[_Y].set( .0,1.0, .0);
		Axis[_Z].set( .0, .0,1.0);
		
		for (int i=0; i<Num_STEP; ++i)	{
			m_angle[i] = 0.0;	m_ax[i] = _X;
		}
		
		ShowWindow(SW_SHOW); 		return TRUE;
	}
	else return false;	
}

void CDLG_CameraSetting::Re_display(){
	CWnd::ShowWindow(SW_SHOW);
}

void CDLG_CameraSetting::DoDataExchange(CDataExchange* pDX){
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_STEP1_ANGLE,EDTStepAngle[0]);
	DDX_Control(pDX, IDC_EDIT_STEP2_ANGLE,EDTStepAngle[1]);
	DDX_Control(pDX, IDC_EDIT_STEP3_ANGLE,EDTStepAngle[2]);
	
	DDX_Control(pDX,IDC_RADIO_STEP1_X,BTNStep1Axis[0]);
	DDX_Control(pDX,IDC_RADIO_STEP1_Y,BTNStep1Axis[1]);
	DDX_Control(pDX,IDC_RADIO_STEP1_Z,BTNStep1Axis[2]);

	DDX_Control(pDX,IDC_RADIO_STEP2_X,BTNStep2Axis[0]);
	DDX_Control(pDX,IDC_RADIO_STEP2_Y,BTNStep2Axis[1]);
	DDX_Control(pDX,IDC_RADIO_STEP2_Z,BTNStep2Axis[2]);

	DDX_Control(pDX,IDC_RADIO_STEP3_X,BTNStep3Axis[0]);
	DDX_Control(pDX,IDC_RADIO_STEP3_Y,BTNStep3Axis[1]);
	DDX_Control(pDX,IDC_RADIO_STEP3_Z,BTNStep3Axis[2]);
}

BEGIN_MESSAGE_MAP(CDLG_CameraSetting, CDialog)
	ON_BN_CLICKED(IDC_RADIO_STEP1_X, &OnBnClickedRadioStep1X)
	ON_BN_CLICKED(IDC_RADIO_STEP1_Y, &OnBnClickedRadioStep1Y)
	ON_BN_CLICKED(IDC_RADIO_STEP1_Z, &OnBnClickedRadioStep1Z)
	ON_BN_CLICKED(IDC_RADIO_STEP2_X, &OnBnClickedRadioStep2X)
	ON_BN_CLICKED(IDC_RADIO_STEP2_Y, &OnBnClickedRadioStep2Y)
	ON_BN_CLICKED(IDC_RADIO_STEP2_Z, &OnBnClickedRadioStep2Z)
	ON_BN_CLICKED(IDC_RADIO_STEP3_X, &OnBnClickedRadioStep3X)
	ON_BN_CLICKED(IDC_RADIO_STEP3_Y, &OnBnClickedRadioStep3Y)
	ON_BN_CLICKED(IDC_RADIO_STEP3_Z, &OnBnClickedRadioStep3Z)
	
	ON_EN_CHANGE(IDC_EDIT_STEP1_ANGLE,	&OnEnChangeAngleStep1)
	ON_EN_CHANGE(IDC_EDIT_STEP2_ANGLE,	&OnEnChangeAngleStep2)
	ON_EN_CHANGE(IDC_EDIT_STEP3_ANGLE,	&OnEnChangeAngleStep3)

	ON_BN_CLICKED(IDOK,				&OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_APPLY,	&OnBnClickedOk)	
	ON_BN_CLICKED(IDCANCEL,			&OnBnClickedCancel)

END_MESSAGE_MAP()

void CDLG_CameraSetting::OnBnClickedRadioStep1X(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep1Y(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep1Z(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep2X(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep2Y(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep2Z(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep3X(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep3Y(){	}
void CDLG_CameraSetting::OnBnClickedRadioStep3Z(){	}

void CDLG_CameraSetting::OnBnClickedOk()
{
	for (int i=0; i<Num_STEP; ++i){
// 		m_angle[i] = 0.0;
// 		m_ax[0] = 0;
// 		EDTStepAngle[i].SetWindowText("0");
	}
	
	OnOK();
}

void CDLG_CameraSetting::OnBnClickedCancel()
{
	OnCancel();
}

void CDLG_CameraSetting::calcRotation(const CEdit &angleEDT, const CButton *axisBTN, float &angle, int &ax){
	
	CString s;		
	angleEDT.GetWindowText(s);
	angle=atof(s);

	if(axisBTN[_X].GetCheck())			ax = _X;
	else if(axisBTN[_Y].GetCheck())		ax = _Y;
	else if(axisBTN[_Z].GetCheck())		ax = _Z;
	else	angle = 0;		
}

void CDLG_CameraSetting::OnEnChangeAngleStep1()
{
	m_pView->m_pTrackBall->Rotate(-m_angle[2], Axis[m_ax[2]]);
	m_pView->m_pTrackBall->Rotate(-m_angle[1], Axis[m_ax[1]]);
	m_pView->m_pTrackBall->Rotate(-m_angle[0], Axis[m_ax[0]]);
	calcRotation(EDTStepAngle[0], BTNStep1Axis, m_angle[0], m_ax[0]);	
	m_pView->m_pTrackBall->Rotate(m_angle[0], Axis[m_ax[0]]);
	m_pView->m_pTrackBall->Rotate(m_angle[1], Axis[m_ax[1]]);
	m_pView->m_pTrackBall->Rotate(m_angle[2], Axis[m_ax[2]]);
	m_pView->InvalidateRect(NULL, FALSE);
}

void CDLG_CameraSetting::OnEnChangeAngleStep2()
{
	m_pView->m_pTrackBall->Rotate(-m_angle[2], Axis[m_ax[2]]);
	m_pView->m_pTrackBall->Rotate(-m_angle[1], Axis[m_ax[1]]);
	calcRotation(EDTStepAngle[1], BTNStep2Axis, m_angle[1], m_ax[1]);	
	m_pView->m_pTrackBall->Rotate(m_angle[1], Axis[m_ax[1]]);
	m_pView->m_pTrackBall->Rotate(m_angle[2], Axis[m_ax[2]]);
	m_pView->InvalidateRect(NULL, FALSE);
}

void CDLG_CameraSetting::OnEnChangeAngleStep3()
{
	m_pView->m_pTrackBall->Rotate(-m_angle[2], Axis[m_ax[2]]);
	calcRotation(EDTStepAngle[2], BTNStep3Axis, m_angle[2], m_ax[2]);	
	m_pView->m_pTrackBall->Rotate(m_angle[2], Axis[m_ax[2]]);
	m_pView->InvalidateRect(NULL, FALSE);
}